package com.ruoyi.hcrf.domain.service.agv.impl;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.hcrf.bo.input.storage.StockFlowLogPageInBo;
import com.ruoyi.hcrf.domain.StockMaster;
import com.ruoyi.hcrf.domain.entity.task.AgvMonious;
import com.ruoyi.hcrf.domain.entity.task.InOut;
import com.ruoyi.hcrf.domain.mapper.agv.AgvMoniousMapper;
import com.ruoyi.hcrf.domain.mapper.storage.StockMasterMapper;
import com.ruoyi.hcrf.domain.service.agv.AgvMoniousService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AgvMoniousServiceImpl extends ServiceImpl<AgvMoniousMapper, AgvMonious> implements AgvMoniousService {

    @Autowired
    private AgvMoniousMapper agvMoniousMapper;

    @Autowired
    private StockMasterMapper stockMasterMapper;

    /**
     * 分页查询agf入库单
     * @param pageInBo
     * @return
     */

    @Override
    public Page<StockMaster> findAllByPage(StockFlowLogPageInBo pageInBo) {

        QueryWrapper<StockMaster> queryWrapper = new QueryWrapper<>();

        //创建查询构造条件
        getQueryMapper(pageInBo, queryWrapper);

        Page<StockMaster> page = new Page<>(pageInBo.getPageNum(), pageInBo.getPageSize());

        return stockMasterMapper.selectPage(page, queryWrapper);

    }

    /**
     * 分页查询agV入库单
     * @param pageInBo
     * @return
     */
    @Override
    public Page<StockMaster> findAllByAgvPage(StockFlowLogPageInBo pageInBo) {
        QueryWrapper<StockMaster> queryWrapper = new QueryWrapper<>();

        //创建查询构造条件
        getAgvQueryMapper(pageInBo, queryWrapper);

        Page<StockMaster> page = new Page<>(pageInBo.getPageNum(), pageInBo.getPageSize());

        return stockMasterMapper.selectPage(page, queryWrapper);
    }

    private void getAgvQueryMapper(StockFlowLogPageInBo pageInBo, QueryWrapper<StockMaster> queryWrapper) {
        queryWrapper.in("ware_house", 4);

        //
        if (StringUtils.isNotBlank(pageInBo.getLocationId()) && StringUtils.isNotBlank(pageInBo.getLocationId().trim())) {
            queryWrapper.like("location_id", pageInBo.getLocationId());
        }
        if (StringUtils.isNotBlank(pageInBo.getLocationGoods()) && StringUtils.isNotBlank(pageInBo.getLocationGoods().trim())) {
            queryWrapper.like("location_goods", pageInBo.getLocationGoods());
        }
        if (pageInBo.getWareHouse() != null) {
            queryWrapper.eq("ware_house", pageInBo.getWareHouse());
        }
        if (pageInBo.getStorageEnabled() != null) {
            queryWrapper.eq("storage_enabled", pageInBo.getStorageEnabled());
        }
        if (pageInBo.getStockFlowType() != null) {
            queryWrapper.eq("storage_status", pageInBo.getStockFlowType());
        }

        //创建时间排序
        queryWrapper.orderByDesc("create_time");
    }

    private void getQueryMapper(StockFlowLogPageInBo pageInBo, QueryWrapper<StockMaster> queryWrapper) {

        queryWrapper.in("ware_house", 1,2,3);

        //
        if (StringUtils.isNotBlank(pageInBo.getLocationId()) && StringUtils.isNotBlank(pageInBo.getLocationId().trim())) {
            queryWrapper.like("location_id", pageInBo.getLocationId());
        }
        if (StringUtils.isNotBlank(pageInBo.getLocationGoods()) && StringUtils.isNotBlank(pageInBo.getLocationGoods().trim())) {
            queryWrapper.like("location_goods", pageInBo.getLocationGoods());
        }
        if (pageInBo.getWareHouse() != null) {
            queryWrapper.eq("ware_house", pageInBo.getWareHouse());
        }
        if (pageInBo.getStorageEnabled() != null) {
            queryWrapper.eq("storage_enabled", pageInBo.getStorageEnabled());
        }
        if (pageInBo.getStockFlowType() != null) {
            queryWrapper.eq("storage_status", pageInBo.getStockFlowType());
        }

        //创建时间排序
        queryWrapper.orderByDesc("create_time");


    }
}
